﻿<sap:ActivityDesigner x:Class="Microsoft.Samples.SqlServer.Activities.Designers.ConnectionActivityDesigner"
    xmlns:s="clr-namespace:System;assembly=mscorlib" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
    xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation" 
    xmlns:net="clr-namespace:System.Net;assembly=System" 
    xmlns:sql="clr-namespace:System.Data.SqlClient;assembly=System.Data" Collapsible="True" Loaded="ActivityDesigner_Loaded">
    <sap:ActivityDesigner.Resources>
        <sapc:ArgumentToExpressionConverter 
                x:Key="ArgumentToExpressionConverter" />

        <DataTemplate x:Key="ShowAsCollapsed">
            <TextBlock Foreground="Gray">
                <TextBlock.Text>
                     <MultiBinding StringFormat="Connect to {0}">
                        <Binding Path="ModelItem.FriendlyName.Expression" />       
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
        
        <DataTemplate x:Key="ShowAsExpanded">
            <Grid>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition />
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition />
                </Grid.RowDefinitions>

                <TextBlock Text="Credentials" Grid.Row ="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" />

                <sapv:ExpressionTextBox HintText="Credential" 
                    Grid.Row ="0" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                    OwnerActivity="{Binding Path=ModelItem}"
                    Expression="{Binding Path=ModelItem.NetworkCredential, Mode=TwoWay, 
                    Converter={StaticResource ArgumentToExpressionConverter}, 
                    ConverterParameter=In }" ExpressionType="net:ICredentials" />

                <TextBlock Text="Server Name" Grid.Row ="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" />

                <sapv:ExpressionTextBox HintText="Server Name" 
                    Grid.Row ="1" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                    OwnerActivity="{Binding Path=ModelItem}"
                    Expression="{Binding Path=ModelItem.DataSource, Mode=TwoWay, 
                    Converter={StaticResource ArgumentToExpressionConverter}, 
                    ConverterParameter=In }" ExpressionType="s:String" />

                <TextBlock Text="Database" Grid.Row ="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" />

                <sapv:ExpressionTextBox HintText="Database" 
                    Grid.Row ="2" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                    OwnerActivity="{Binding Path=ModelItem}"
                    Expression="{Binding Path=ModelItem.InitialCatalog, Mode=TwoWay, 
                    Converter={StaticResource ArgumentToExpressionConverter}, 
                    ConverterParameter=In }" ExpressionType="s:String" />

                <TextBlock Text="Friendly Name" Grid.Row ="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" />
                <sapv:ExpressionTextBox HintText="Friendly Name" 
                    Grid.Row ="3" Grid.Column="2" MaxWidth="150" MinWidth="150" Margin="5"
                    OwnerActivity="{Binding Path=ModelItem}"
                    Expression="{Binding Path=ModelItem.FriendlyName, Mode=TwoWay, 
                    Converter={StaticResource ArgumentToExpressionConverter}, 
                    ConverterParameter=In }" ExpressionType="s:String" />

                <TextBlock Text="=" TextAlignment="Right" Grid.Column="1" Grid.Row="4"  Margin="5"/>
                <sapv:ExpressionTextBox HintText="SqlConnectionBuilder" 
                    Grid.Row ="4" Grid.Column="2" Margin="4" MaxWidth="200" MinWidth="150"
                    OwnerActivity="{Binding Path=ModelItem}"
                    Expression="{Binding Path=ModelItem.SqlConnectionBuilder, Mode=TwoWay, 
                    Converter={StaticResource ArgumentToExpressionConverter}, 
                    ConverterParameter=Out }"
                    ExpressionType="sql:SqlConnectionStringBuilder" UseLocationExpression="True">
                </sapv:ExpressionTextBox>
            </Grid>
        </DataTemplate>

        <Style x:Key="StyleWithCollapse" TargetType="{x:Type ContentPresenter}">
            <Setter Property="ContentTemplate" 
                Value="{DynamicResource ShowAsExpanded}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=ShowExpanded}" Value="False">
                    <Setter Property="ContentTemplate" 
                        Value="{DynamicResource ShowAsCollapsed }"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </sap:ActivityDesigner.Resources>

    <sap:ActivityDesigner.Icon>
        <DrawingBrush>
            <DrawingBrush.Drawing>
                <ImageDrawing>
                    <ImageDrawing.Rect>
                        <Rect Location="0,0"  Size="16,16" />
                    </ImageDrawing.Rect>
                    <ImageDrawing.ImageSource>

                        <BitmapImage UriSource="ConnectionBuilderActivity.png"/>
                    </ImageDrawing.ImageSource>
                </ImageDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </sap:ActivityDesigner.Icon>

    <Grid>
        <ContentPresenter Style="{DynamicResource StyleWithCollapse}" 
            Content="{Binding}" />

    </Grid>
</sap:ActivityDesigner>
